//
//  MineViewController.m
//  小汽车
//
//  Created by mill on 15/1/7.
//  Copyright (c) 2015年 蓝欧科技. All rights reserved.
//

#import "MineViewController.h"
#import "LandViewController.h"
#import "NightModeViewController.h"
#import "CollectionsViewController.h"
#import "CustomView.h"
#import "Singleton.h"
#import "CustomCollectionViewCell.h"
#import "SetViewController.h"
#import "UserDetailViewController.h"
#import "UserDetailModel.h"
#import "UIImageView+WebCache.h"
@interface MineViewController ()<LandViewControllerDelegate,SetViewControllerDelegate,UserDetailViewControllerDeletage>

@end

@implementation MineViewController

- (void)dealloc
{
    [_tableView release];
    [_imageArray release];
    [_lableArray release];
    [_landView release];
    [_userBigButton release];
    [_registerbutton release];
    [_userPicture release];
    [_userId release];
    [_coin release];
    [_level release];
    
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.imageArray = [NSMutableArray array];
        self.lableArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createNavigation];
    [self createTabelView];
    //    [self createCollectionView];
    [self newCreateView];
    [self createButton];
   
    
}
-(void)createNavigation
{
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBarHidden = NO;
    self.navigationItem.title = @"我";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"设置" style:UIBarButtonItemStylePlain target:self action:@selector(SetUpAction:)];
}

-(void)createTabelView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.tableView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:0.95];
    
    [self.view addSubview:_tableView];
    
    self.landView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height/ 10 * 3)];
    _landView.backgroundColor = [UIColor cyanColor];
    _tableView.tableHeaderView = _landView;
    
    
    [_landView release];
    
    
    [_tableView release];
    
}

-(void)SetUpAction:(UIButton *)button
{
    SetViewController *setup = [[SetViewController alloc] init];
    setup.nicname = self.myModel.nickname;
    setup.registTime = self.myModel.registTime;
    setup.sex = self.myModel.sex;
    setup.delegate = self;
    [self.navigationController pushViewController:setup animated:YES];
}


-(void)newCreateView
{
    Singleton *share = [Singleton shareInstance];
    if (share.special == NO) {
       
        self.registerbutton = [[UIButton alloc] init];
        _registerbutton.frame = CGRectMake(0, 0, self.view.frame.size.width/4, self.view.frame.size.height / 20);
        _registerbutton.center = _landView.center ;
        [_registerbutton setTitle:@"登录/注册" forState:UIControlStateNormal];
        _registerbutton.layer.borderWidth = 1.5;
        [_registerbutton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
        
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
        CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 1, 1 });
        [_registerbutton.layer setBorderColor:colorref];//边框颜色
        
        [_registerbutton addTarget:self action:@selector(landAction:) forControlEvents:UIControlEventTouchUpInside];
        [_tableView addSubview:_registerbutton];
        [_registerbutton release];
    } else{
        self.exist = YES;
        self.userBigButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 12, 0, self.view.frame.size.width / 5 * 4, self.view.frame.size.height / 4)];
        [self.userBigButton addTarget:self action:@selector(userDetailAction:) forControlEvents:UIControlEventTouchUpInside];
        _userBigButton.center = _landView.center;
        [_tableView addSubview:_userBigButton];
        [_userBigButton release];
        
        self.userPicture = [[UIImageView alloc] initWithFrame:CGRectMake(self.userBigButton.frame.size.width / 18, self.userBigButton.frame.size.height / 8, self.userBigButton.frame.size.width / 16 * 5, self.userBigButton.frame.size.height / 4 * 3)];
        if (!_userPicture.image) {
            [self.userPicture setImageWithURL:[NSURL URLWithString:self.myModel.profileImage] placeholderImage:[UIImage imageNamed:@"placeHolder.png"] options:SDWebImageRetryFailed];
        }
        
        _userPicture.backgroundColor = [UIColor yellowColor];
        [_userBigButton addSubview:_userPicture];
        
        
        self.userId = [[UILabel alloc] initWithFrame:CGRectMake(self.userBigButton.frame.size.width / 18 * 7, self.userBigButton.frame.size.height / 8, self.userBigButton.frame.size.width / 9 * 5, self.userBigButton.frame.size.height / 16 * 3)];
        
        _userId.text = [NSString stringWithFormat:@"car%@",self.myModel.uid];
        _userId.font = [UIFont fontWithName:@"Helvetica-Bold" size:16];
        _userId.textColor = [UIColor whiteColor];
        [_userBigButton addSubview:_userId];
        
        UIImage * image = [UIImage imageNamed:@"detail.png"];
        UIButton *arrowButton = [[UIButton alloc] initWithFrame:CGRectMake(self.userBigButton.frame.size.width / 36 * 31, self.userBigButton.frame.size.height / 32 * 13, self.userBigButton.frame.size.height / 16 * 3, self.userBigButton.frame.size.height / 16 * 3)];
        [arrowButton setBackgroundImage:image forState:UIControlStateNormal];
        [arrowButton addTarget:self action:@selector(userDetailAction:) forControlEvents:UIControlEventTouchUpInside];
        self.coin = [[UILabel alloc] initWithFrame:CGRectMake(self.userBigButton.frame.size.width / 18 * 7, self.userBigButton.frame.size.height / 32 * 13, self.userBigButton.frame.size.width / 36 * 17, self.userBigButton.frame.size.height / 16 * 3)];
        if(self.changeCarCoin == NO){
        NSInteger carCoinOld = 48;
            self.carCoin = carCoinOld;
        }
        [self.coin setText:[NSString stringWithFormat:@"汽车币 :%ld",_carCoin]];
        _coin.font = [UIFont fontWithName:@"Helvetica-Bold" size:16];
        _coin.textColor = [UIColor whiteColor];
        [_userBigButton addSubview:arrowButton];
        [_userBigButton addSubview:_coin];
        
        self.level = [[UILabel alloc] initWithFrame:CGRectMake(self.userBigButton.frame.size.width / 18 * 7, self.userBigButton.frame.size.height / 32 * 22, self.userBigButton.frame.size.width / 9 * 5, self.userBigButton.frame.size.height / 16 * 3)];
        if (_carCoin < 50) {
            _level.text = @"新手上路";
        }else if(_carCoin >= 50 && _carCoin < 100){
            _level.text = @"小有成就";
        }else if(_carCoin >= 100 && _carCoin < 200){
            _level.text = @"初级用户";
        }else{
            _level.text = @"中级用户";
        }
        _level.textColor = [UIColor whiteColor];
        _level.font = [UIFont fontWithName:@"Helvetica-Bold" size:16];
        [_userBigButton addSubview:_level];
        
        [_userPicture release];
        [arrowButton release];
        [_userId release];
        [_coin release];
        [_level release];
        
    }
    
}

-(void)landAction:(UIButton *)button
{
    Singleton *share = [Singleton shareInstance];
    if (share.special == NO) {
        LandViewController *land = [[LandViewController alloc] init];
        land.delegate = self;
        [self.navigationController pushViewController:land animated:YES];
        [land release];
    }else{
        UserDetailViewController *userDetail = [[UserDetailViewController alloc] init];
        [self.navigationController pushViewController:userDetail animated:YES];
        [userDetail release];
    }
    
}

-(void)createButton
{
    self.lableArray =[NSMutableArray arrayWithObjects:@"我的收藏",@"夜间模式",@"签到", nil];
    self.imageArray =[NSMutableArray arrayWithObjects:@"collection1.png", @"night.png", @"sign.png", nil];
    for (int i = 0; i < [_lableArray count]; i++) {
        CustomView *custom = [[CustomView alloc] initWithFrame:CGRectMake( 80 *i, self.view.frame.size.height / 10 *3, self.view.frame.size.width / 14 * 3, self.view.frame.size.width / 14 * 3)];
        custom.backgroundColor = [UIColor whiteColor];
        custom.label.text = [_lableArray objectAtIndex:i];
        UIImage *image = [UIImage imageNamed:[_imageArray objectAtIndex:i]];
        custom.imageView.image = image;
        custom.button.layer.borderWidth = 0.3;
        custom.label.tag = 50 + i ;
        custom.button.tag = 500 + i;
        custom.tag = 100 + i;
        [custom.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 1) {
            
            UIView *myView = [[UIView alloc] initWithFrame:CGRectMake( 80, self.view.frame.size.height / 10 * 3, self.view.frame.size.width / 14 * 3, self.view.frame.size.width / 14 * 3)];
            [myView setBackgroundColor:[UIColor blueColor]];
            
            [custom setFrame:CGRectMake(0, 0, self.view.frame.size.width / 14 * 3, self.view.frame.size.width / 14 * 3)];
            [myView addSubview:custom];
            [self.tableView addSubview:myView];
        }
        else
        {
            [_tableView addSubview:custom];
        }
        
        [custom release];
    }
}
-(void)buttonAction:(UIButton *)button
{
    Singleton *share = [Singleton shareInstance];
    if (button.tag == 500 ) {
        if (share.special == NO) {
            LandViewController *land = [[LandViewController alloc] init];
            land.delegate = self;
            [self.navigationController pushViewController:land animated:YES];
        }else{
            CollectionsViewController *collection = [[CollectionsViewController alloc] init] ;
            
            [self.navigationController pushViewController:collection animated:YES];
        }
        
    }else if(button.tag == 501){
        
        
        
        [self myNightModel];

//        //夜间模式
//        UIWindow *window = [[UIApplication sharedApplication] keyWindow];
//        UITabBarController *tab = (UITabBarController *)window.rootViewController;
//       //在这里用NSUserDefaults该更改夜间模式
//        
//
//        NSUserDefaults *myNight = [NSUserDefaults standardUserDefaults];
//        if (tab.tabBar.barTintColor == [UIColor whiteColor])
//        {
//
//          
//            [myNight setObject:@"1" forKey:@"night"];
//            
//            tab.tabBar.barTintColor = [UIColor blackColor];
//            self.tableView.backgroundColor = [UIColor colorWithRed:0/255.0f green:0/255.0f blue:0/255.0f alpha:1];
//            self.landView.backgroundColor = [UIColor blackColor];
//         
//        
//            //改变button的背景颜色
//            for (int i = 100; i < 103; i++) {
//                CustomView *custom = (CustomView *)[self.tableView viewWithTag:i];
//                custom.backgroundColor = [UIColor grayColor];
//                
//            }
//        }
//        else
//        {
//
//            
//            [myNight setObject:@"0" forKey:@"night"];
//            tab.tabBar.barTintColor = [UIColor whiteColor];
//            self.tableView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:0.95];
//            self.landView.backgroundColor = [UIColor cyanColor];
//            
//            for (int i = 100; i < 103; i++) {
//                CustomView *custom = (CustomView *)[self.tableView viewWithTag:i];
//                custom.backgroundColor = [UIColor whiteColor];
//                
//            }
// 
//        }
//
        
    }else{
        if (share.special == NO) {
            LandViewController *land = [[LandViewController alloc] init];
            land.delegate = self;
            [self.navigationController pushViewController:land animated:YES];
            [land release];
        }else{
            
            UILabel *myLabel = (UILabel *)[self.tableView viewWithTag:52];
            myLabel.text = @"已签到";
            
            //用时间来判断能否点击（但是每次在此button的点击事件中_changeCarCoin必须设置为YES）
            if (self.changeCarCoin == NO) {
                self.changeCarCoin = YES;
                _carCoin = _carCoin + 2;
            }
            _coin.text = [NSString stringWithFormat:@"汽车币: %ld", _carCoin];
            if(_carCoin >= 50 & _carCoin < 100){
                _level.text = @"小有成就";
            }else if (_carCoin >= 100 && _carCoin <200){
                _level.text = @"初级用户";
            }else{
                _level.text = @"中级用户";
            }
            [_tableView reloadData];
        }
    }
}




-(void)changeViewStateUid:(NSString *)uid nicname:(NSString *)nicname profileImage:(NSString *)profileImage sex:(NSString *)sex registTime:(NSString *)registTime
{
    UserDetailModel *model = [[UserDetailModel alloc] init] ;
    
    Singleton *share = [Singleton shareInstance];
    if (share.special == YES) {
        if (self.registerbutton) {
            [self.registerbutton removeFromSuperview];
        }
        if (self.myModel) {
            [self.tableView reloadData];
            [self.userBigButton removeFromSuperview];
            [self newCreateView];
            return;
        }
        model.uid = uid;
        model.nickname = nicname;
        model.profileImage = profileImage;
        model.registTime = registTime;
        model.sex = sex;
        self.myModel = model;
        [self.tableView reloadData];
        [self newCreateView];
    }else{
        if (self.exist == YES) {
            self.exist = NO;
            [self.userBigButton removeFromSuperview];
            [self.tableView reloadData];
            [self newCreateView];
        }
        
       
    }
    [model release];
}

-(void)SendPictureImage:(UIImage *)image1 changeSex:(NSString *)changeSex
{
    _userPicture.image = image1;
    if (changeSex) {
        _myModel.sex = changeSex;
    }
    [_tableView reloadData];
}

- (void)userDetailAction:(UIButton *)button
{
    
    UserDetailViewController *userDetail = [[UserDetailViewController alloc] init];
    userDetail.background = _myModel.profileImage;
    userDetail.uid = _myModel.uid;
    userDetail.level = _level.text;
    userDetail.registTime =  _myModel.registTime;
    userDetail.sex = _myModel.sex;
    userDetail.image1 = _userPicture.image;
    userDetail.delegate = self;
    [self.navigationController pushViewController:userDetail animated:YES];
    [userDetail release];
}

#pragma mark -  夜间模式的过渡模式
- (void)myNightModel
{
    //night
    CustomView *myNightView = [[CustomView alloc] initWithFrame:CGRectMake( 0, 0, self.view.frame.size.width / 14 * 3, self.view.frame.size.width / 14 * 3)];
    myNightView.tag = 132;
    [myNightView setBackgroundColor:[UIColor orangeColor]];
    
   //Day
    CustomView *myDayView = (CustomView *)[self.tableView viewWithTag:101];
    [myNightView.button addTarget:self action:@selector(nightAction:) forControlEvents:UIControlEventTouchUpInside];
    //视图过渡效果
    
    [UIView transitionFromView:myDayView toView:myNightView duration:1.5 options:UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOL finished) {
        //现在的想法是如何让其翻转过来
//        [myNightView setFrame:CGRectMake(0, 0, self.view.frame.size.width / 14 * 3, self.view.frame.size.width / 14 * 3)];
//        UIView *myView = [[UIView alloc] initWithFrame:CGRectMake( 80, self.view.frame.size.height / 10 * 3, self.view.frame.size.width / 14 * 3, self.view.frame.size.width / 14 * 3)];
//        [myView setBackgroundColor:[UIColor blueColor]];
//        [myView addSubview:myNightView];
//       
//        [self.tableView addSubview:myView];
//        [myView release];
    }];
    [myNightView release];
    

}
- (void)nightAction:(id)sender
{
    CustomView *myNightView = (CustomView *)[self.tableView viewWithTag:132];
    CustomView *myDayView = (CustomView *)[self.tableView viewWithTag:101];
//    [myDayView removeFromSuperview];
    [UIView transitionFromView:myNightView toView:myDayView duration:1.5 options:UIViewAnimationOptionTransitionFlipFromRight completion:^(BOOL finished) {
        
//        UIView *myView = [[UIView alloc] initWithFrame:CGRectMake( 80, self.view.frame.size.height / 10 * 3, self.view.frame.size.width / 14 * 3, self.view.frame.size.width / 14 * 3)];
//        [myView setBackgroundColor:[UIColor blueColor]];
//        
//        [myDayView setFrame:CGRectMake(0, 0, self.view.frame.size.width / 14 * 3, self.view.frame.size.width / 14 * 3)];
//        [myView addSubview:myDayView];
//        [myNightView removeFromSuperview];
//        [self.tableView addSubview:myView];

    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
